package com.example.grafstock_upload;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.csi.controle.core.entidade.Permissao;
import org.csi.controle.upload.listener.RetornoLogin;
import org.csi.controle.upload.listener.RetornoPermissoes;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AutenticaActivity extends Activity implements OnClickListener {

	private ProgressDialog dialog;
	
	private Toast toast;
	
	private AutenticaActivity instancia;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		instancia = this;
		try {
			dialog = ProgressDialog.show(AutenticaActivity.this, "", "Aguarde, carregando...", true);
			FileInputStream fis = openFileInput("token_upload_controle");
			byte[] byteToken = new byte[fis.available()];
			fis.read(byteToken);
			fis.close();
			final String token = new String(byteToken);
			ServicoAndroid<List<Permissao>> sa = new ServicoAndroid<List<Permissao>>(new RetornoPermissoes(this));
			String[] params = new String[] {ServicoAndroid.TIPO_OBTER_PERMISSOES, token};
			sa.execute(params);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_autentica);
		Button btn = (Button) this.findViewById(R.id.btnEntrar);
		btn.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		dialog = ProgressDialog.show(AutenticaActivity.this, "", "Aguarde, autenticando...", true);
		EditText txtUsuario = (EditText) this.findViewById(R.id.txtUsuario);
		EditText txtSenha = (EditText) this.findViewById(R.id.txtSenha);
		ServicoAndroid<String> sa = new ServicoAndroid<String>(new RetornoLogin(this));
		String[] params = new String[] {ServicoAndroid.TIPO_LOGIN, txtUsuario.getText().toString(), txtSenha.getText().toString()};
		sa.execute(params);
	}

	public ProgressDialog getDialog() {
		return dialog;
	}

	public Toast getToast() {
		return toast;
	}

}